package org.esprit.ocm.client.ui.ec2;

import org.esprit.ocm.dto.impl.AwsCredentialsDto;
import org.esprit.ocm.dto.impl.ServerDto;
import org.esprit.ocm.dto.impl.UserDto;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.viewer.DetailViewer;

public class AvailabilityZone extends SectionStack {

	private DataSource		ds;
	private ListGrid		grid;
	private DetailViewer	details;

	public AvailabilityZone(final ServerDto server, final AwsCredentialsDto credentials, final UserDto user) {

		getData(server, credentials);

		setVisibilityMode(VisibilityMode.MULTIPLE);
		setHeaderHeight(20);

		// creer instanceSection
		SectionStackSection zoneSection = new SectionStackSection("Zones List");
		zoneSection.setExpanded(true);
		addSection(zoneSection);

		// creer instanceGrid
		grid = new ListGrid();

		// instanceGrid.setShowFilterEditor(true);
		grid.setFilterOnKeypress(true);
		grid.setAutoFetchData(true);

		// creer instancetoolstrip
		ToolStrip zoneGridToolStrip = new ToolStrip();
		zoneGridToolStrip.setWidth100();
		zoneGridToolStrip.addFill();

		// ajouter toolstrip a instanceSection
		zoneSection.addItem(zoneGridToolStrip);

		// creer filterButton
		final Button filterButtonOn = new Button("Filter On");
		filterButtonOn.setShowRollOver(true);
		filterButtonOn.setShowDown(true);
		filterButtonOn.setIcon("filter.png");
		filterButtonOn.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {

				grid.setShowFilterEditor(true);
				grid.setFilterOnKeypress(true);
			}
		});
		// creer filterButton
		final Button filterButtonOff = new Button("Filter Off");
		filterButtonOff.setShowRollOver(true);
		filterButtonOff.setShowDown(true);
		filterButtonOff.setIcon("filter.png");
		filterButtonOff.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {

				grid.setShowFilterEditor(false);
				grid.setFilterOnKeypress(false);
			}
		});

		// ajouter le bouton
		zoneGridToolStrip.addMember(filterButtonOn);
		zoneGridToolStrip.addMember(filterButtonOff);

		/******************************/
		grid.setShowRecordComponents(true);
		grid.setShowRecordComponentsByCell(true);
		grid.setShowAllRecords(true);
		/******************************/

		// affecter les fields de instanceGrid
		ListGridField nameField = new ListGridField("name", "Name");
		ListGridField descriptionField = new ListGridField("description", "Description");

		grid.setFields(nameField, descriptionField);

		// ajouter instanceGrid a instanceSection
		zoneSection.addItem(grid);

		// creer DetailinstanceSection

		details = new DetailViewer();
		details.setWidth100();
		details.setMargin(15);
		details.setEmptyMessage("Select something to view its details");

		// relation entre instancegrid et instancedtails
		grid.addRecordClickHandler(new RecordClickHandler() {

			public void onRecordClick(RecordClickEvent event) {

				details.setData(new Record[] { event.getRecord() });
			}
		});

		// detailsection
		SectionStackSection zoneDetailSection = new SectionStackSection("Zone Detail");
		zoneDetailSection.setExpanded(true);
		zoneDetailSection.addItem(details);
		addSection(zoneDetailSection);

		draw();
	}

	private void getData(ServerDto server, AwsCredentialsDto credentials) {
	}
}
